/* ttChatmode 
   by NOPERATION 
   2012.08.02
   */

if($('#upboat').length < 1) { /* don't reload */

    var api = (function(){ 
        var apiRegex = / Preparing message /i;
        for (var memberName in turntable) {
            var member = turntable[memberName];
            if (typeof member !== 'function') continue;
            member.toString = Function.prototype.toString;
            if (apiRegex.test(member.toString())) {
                return member;
            }
        }
        return false;
    })();
    var room = (function() {
        for (var memberName in turntable) {
            var member = turntable[memberName];
            if (typeof member !== 'object' || member === null) continue;
            if (typeof member.setupRoom !== 'undefined') {
                return member;
            }
        }
        return false;
    })();
    var getDJlist = function() {
        for (var memberName in room) {
            var member = room[memberName];
            if (typeof member !== 'object' || member === null) continue;
            if (typeof member.djs !== 'undefined') {
                return member.djs;
            }
        }
        return false;
    };

    var chat_container = $($('.chat-container')[0]);

    $('html > head').append($('<style>.roomView .chat-container .messages .message { width: auto; } '+
                            '.chat-container { z-index: 10001; top: 320px !important; '+
                            'width: 527px !important; height: 383px !important; } '+
                            '.roomView .messages { height: 321px !important; } '+
                            '#upboat, #downboat { position: absolute; top: 672px; z-index: 25002; '+
                            'line-height: 25px; width: 25px; text-align: center; cursor: pointer; } '+
                            '#upboat:hover, #downboat:hover { background: #B0B0B0; } '+
                            '#upboat { left: 530px; color: #009000; } #downboat { left: 500px; color: #900000; } '+
                            '#upboat:hover { color: #00FF00; } #downboat:hover { color: #FF0000; } '+
                            '.speak_bubble, .laptopCanvas, .bodyback, .bodyfront, .headfront, .headback, .torso, .leftarm, '+
                            '.rightarm, .legs, .fronttorso, .leftarm_front, .rightarm_front, .frontlegs, .backtorso { display: none; } '+
                            '#songboard { color: #fff; } #songboard > div { text-shadow: none; }'+
                            '#djz { position: absolute; top: 95px; padding: 50px; list-style: none;} '+
                            '#djz li { width: 70px; float: left; text-align: center; color: #fff; z-index: 25001; '+
                            'font-size: 10px; margin-left: 5px; } .hidedj { visibility: hidden; } '+
                            '.upvoted { background: #209020; } .chat-container .chatHeader { cursor: default; }'+
                            '.chat-container .chatResizeIcon { display: none; }'+
                            '.chat-container #voteCount { display: none; } </style>'));

    $($('.chatHeader')[0]).unbind('dragenter dragexit dragleave dragover mousedown mousemove mouseout mouseup');
    
    var wallpaper = $('img[src="https://s3.amazonaws.com/static.turntable.fm/roommanager_assets/props/wallpaper.png"]');
    wallpaper.parent().css("background","#000");
    wallpaper.remove();
    
    $('.guestListButton').unbind('click').hide();
    $('.guest-list-container').css('margin-left','0px');
    $('.chatBar', chat_container).css('width','527px');
    $('.input-box', chat_container).css({'background':'none','left':'0px'});
    $('input', chat_container).css({'background': '#fff',
                                    'border': '1px solid #7B7B7B',
                                    'width': '475px'});

    var upvote = $('<a id="upboat">&#9650;</a>');
    $('.roomView').append(upvote);
    var downvote = $('<a id="downboat">&#9660;</a>');
    $('.roomView').append(downvote);

    upvote.bind("click",function(){
        api({
            api: 'room.vote',
            roomid: room.roomId,
            val: 'up',
            vh: $.sha1(room.roomId + 'up' + room.currentSong._id),
            th: $.sha1(Math.random() + ''),
            ph: $.sha1(Math.random() + '')
        });
    });
    downvote.bind("click",function(){
        api({
            api: 'room.vote',
            roomid: room.roomId,
            val: 'down',
            vh: $.sha1(room.roomId + 'down' + room.currentSong._id),
            th: $.sha1(Math.random() + ''),
            ph: $.sha1(Math.random() + '')
        });
    });

    $('.roomView').append(chat_container);
    $('.messages').scrollTop(999999);

    $('.roomView').append($('<ul id="djz"><li class="tooltip_info"></li><li class="tooltip_info"></li>'+
                            '<li class="tooltip_info"></li><li class="tooltip_info"></li>'+
                            '<li class="tooltip_info"></li></ul>'));

    var update_names = function(msg){
        if(!msg.command || !/(add_dj|rem_dj|update_votes|newsong)/.test(msg.command))
            return;
        window.setTimeout(function(){
            var list = getDJlist();
            var djz = $('#djz').children();
            for(var i = 0; i < 5; i++) {
                var thisdj = djz[i];
                if(list[i] != undefined) {
                    listitem = list[i];
                    thisdj.innerHTML = listitem[4]['name'];
                    thisdj.className = (listitem[1]['anistate']!=null) ? "tooltip_info upvoted":"tooltip_info";
                } else
                    thisdj.className = "hidedj";
            }
        },0);
    };
    turntable.addEventListener("message",update_names);
    update_names({command:"add_dj"});
}